package com.zx.common;

import com.zx.entity.Category;
import com.zx.entity.Collect;
import com.zx.entity.Link;
import com.zx.entity.User;
import com.zx.repository.CategoryRepository;
import com.zx.repository.CollectRepository;
import com.zx.repository.LinkRepository;
import com.zx.service.UserService;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

public class commonMethod {
    public static void common(Map<String, Object> map, HttpSession session, UserService userService, CategoryRepository repository, LinkRepository linkRepository, CollectRepository collectRepository) {
        String username = (String) session.getAttribute("loginUser");
        if(username!=null){
            User user = userService.findUserInfoByUsername(username);
            List<Collect> collects = collectRepository.findAllByUserId(user.getUserId());
            map.put("collects",collects);
            map.put("user",user);
        }else{
            User user = new User();
            user.setRole(1);
            user.setUsername("nobody");
            map.put("user",user);
        }
        List<Link> link = linkRepository.findAll();
        List<Category> category = repository.findAll();
        map.put("category",category);
        map.put("link",link);
    }
}
